package com.kaizzi.data.jdl.helper;

import com.jfinal.json.Json;
import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.TeamListService;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/16
 */
@Component
public class PageData30 {

    @Resource
    private TeamListService teamListService;

    public String getData(ExcelModel model){

        Page30 page30 = new Page30();
//        List<String> dateList = DateUtil.getDateList(1);
//        List<String> transTime = DateUtil.getTransTime(dateList.get(0));
//        String yearFirstDate = DateUtil.getYearFirstDate();
        List<String> ytdDay = DateUtil.getYtdDay1();

        List<TempBeen> ytdTypeCount = teamListService.getYtdTypeCount(ytdDay.get(0), ytdDay.get(1));
        List<TempBeen> teamSuccessCount = teamListService.getTeamSuccessCount(ytdDay.get(0), ytdDay.get(1));
        List<TempBeen> teamFailCount = teamListService.getTeamFailCount(ytdDay.get(0), ytdDay.get(1));

        //按成功匹配
        List<TempBeen> complexFailCount = getComplexFailCount(teamSuccessCount,teamFailCount);


        page30.setTeamTypeCount(ytdTypeCount);
        page30.setTeamSuccessCount(teamSuccessCount);
        page30.setTeamFailCount(complexFailCount);

        page30.setTitle(LogicUtil.getTitle(model,30));

        List<TempBeen> successTeam = ytdTypeCount.stream().filter(been -> been.getTempName().equals("组队成功")).collect(toList());
        List<TempBeen> failTeam = ytdTypeCount.stream().filter(been -> been.getTempName().equals("组队失败")).collect(toList());
        double sum = ytdTypeCount.stream().mapToDouble(TempBeen::getTempCount).sum();
        String successRate = "0";
        if (successTeam.size() > 0 && sum != 0){
            successRate = StringUtil.pointFormat((double) successTeam.get(0).getTempCount() / sum * 100);
        }
        String failRate = "";
        if (failTeam.size() > 0 && sum != 0){
            failRate = StringUtil.pointFormat((double) failTeam.get(0).getTempCount() / sum * 100);
        }
        String typeTitle = "累计数据表现：有'"+successRate+"%'的队伍组队成功，'"+failRate+"%'的队伍组队失败";
        page30.setTypeTitle(typeTitle);

        return JsonUtil.serialize(page30);
    }

    private List<TempBeen> getComplexFailCount(List<TempBeen> teamSuccessCount, List<TempBeen> teamFailCount) {
        //成功名称集合
        List<String> nameList = new ArrayList<>();
        for (TempBeen been : teamSuccessCount) {
            String tempName = been.getTempName();
            nameList.add(tempName);
        }
        //可以和上面整合一处 ，我懒得写 Kzen
        List<TempBeen> complexFailCount = new ArrayList<>();
        for (String name : nameList) {
            TempBeen been = new TempBeen();
            been.setTempName(name);
            List<TempBeen> tempFail = teamFailCount.stream().filter(tempBeen -> tempBeen.getTempName().equals(name)).collect(toList());
            if (CollectionUtils.isEmpty(tempFail)){
                been.setTempCount(0);
            }else {
                been.setTempCount(tempFail.get(0).getTempCount());
            }
            complexFailCount.add(been);
        }

        return complexFailCount;
    }

    @Data
    private static class Page30{
        private List<TempBeen> teamTypeCount;
        private List<TempBeen> teamSuccessCount;
        private List<TempBeen> teamFailCount;
        private String title;
        private String dataPeriod;
        private String typeTitle;
    }

}
